<template>
  <!-- 气泡背景 -->
  <div class="bruce">
    <ul class="bubble-bgwall">
      <li>General</li>
      <li>Admin</li>
      <li>System</li>
      <li>Login</li>
      <li>Welcome</li>
      <li>Safe</li>
      <li>Modern</li>
      <li>Fast</li>
      <li>Smart</li>
      <li>Cloud</li>
    </ul>
  </div>

  <!-- 登录容器 -->
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>后台管理系统</h1>
        <p class="subtitle">现代化管理平台</p>
        
        <!-- 测试账号提示 -->
        <div class="test-accounts">
          <p class="test-title">📝 数据库账号</p>
          <p class="test-account">超级管理员: superadmin / Admin@123456</p>
          <p class="test-warning">⚠️ 请使用以上凭据登录以查看真实数据库数据</p>
        </div>
        
        <!-- 最近注册用户提示 -->
        <div v-if="lastRegisteredUser" class="recent-user">
          <p class="recent-title">🎉 刚刚注册成功</p>
          <p class="recent-account">用户名: {{ lastRegisteredUser }}</p>
          <p class="recent-note">可使用此用户名和任意密码登录</p>
        </div>
      </div>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            id="username" 
            v-model="username" 
            type="text" 
            placeholder="请输入用户名" 
            required 
          />
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <div class="password-container">
            <input 
              id="password" 
              v-model="password" 
              :type="showPassword ? 'text' : 'password'" 
              placeholder="请输入密码"
              required 
            />
            <button 
              type="button" 
              class="eye-button" 
              @click="showPassword = !showPassword"
            >
              <span v-if="showPassword">👁️</span>
              <span v-else>👁️‍🗨️</span>
            </button>
          </div>
        </div>

        <div class="form-footer">
          <div class="remember-me">
            <input id="remember" type="checkbox" v-model="rememberMe" />
            <label for="remember">记住我</label>
          </div>
          <a href="#" class="forgot-password" @click="handleForgotPassword">忘记密码?</a>
        </div>

        <div class="button-container">
          <button type="submit" class="login-button" :disabled="loading">
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </button>
          <button type="button" @click="handleRegister" class="register-button">
            注册
          </button>
        </div>

        <div v-if="error" class="error-message">
          {{ error }}
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { ElMessage } from 'element-plus';

const router = useRouter();
const userStore = useUserStore();

const username = ref('');
const password = ref('');
const rememberMe = ref(false);
const error = ref('');
const showPassword = ref(false);
const loading = ref(false);
const lastRegisteredUser = ref('');

// 检查是否已有token
onMounted(() => {
  // 获取最近注册的用户名
  const recentUser = localStorage.getItem('lastRegisteredUser');
  if (recentUser) {
    lastRegisteredUser.value = recentUser;
    // 预填用户名
    username.value = recentUser;
    // 清除最近注册用户记录（可选，避免一直显示）
    setTimeout(() => {
      localStorage.removeItem('lastRegisteredUser');
      lastRegisteredUser.value = '';
    }, 30000); // 30秒后清除
  }
  
  // 如果是直接访问登录页面，清除所有登录状态
  if (router.currentRoute.value.path === '/login') {
    // 清除登录状态
    localStorage.removeItem('token');
    localStorage.removeItem('userInfo');
    localStorage.removeItem('permissions');
    // 重置store状态
    userStore.$reset && userStore.$reset();
    userStore.token = '';
    userStore.userInfo = null;
    userStore.permissions = [];
  } else if (localStorage.getItem('token')) {
    // 如果不是登录页面且有token，则跳转到首页
    router.push('/home');
  }
});

const handleLogin = async () => {
  console.log('Attempting login with:', username.value, password.value);
  
  try {
    error.value = '';
    loading.value = true;

    await userStore.login({
      username: username.value,
      password: password.value
    });

    // 如果选择记住我，可以在这里设置更长的过期时间
    if (rememberMe.value) {
      localStorage.setItem('rememberMe', 'true');
    }

    ElMessage.success('登录成功');
    router.push('/home');
  } catch (err) {
    console.error('Login error:', err);
    error.value = err.message || '登录失败，请检查用户名和密码';
    ElMessage.error(error.value);
  } finally {
    loading.value = false;
  }
};

const handleRegister = () => {
  router.push('/register');
};

const handleForgotPassword = (event) => {
  event.preventDefault();
  router.push('/forgot-password');
};
</script>

<style scoped>
/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 气泡背景样式 */
.bruce {
  height: 100vh;
  width: 100vw;
  background-image: linear-gradient(270deg, #8146b4, #6990f6);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  overflow: hidden;
}

.bubble-bgwall {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  list-style: none;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  font-size: 14px;
  font-weight: 500;
  animation: bubble 15s infinite;
}

.bubble-bgwall li:nth-child(1) {
  left: 10%;
}

.bubble-bgwall li:nth-child(2) {
  left: 20%;
  width: 90px;
  height: 90px;
  animation-duration: 7s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(3) {
  left: 25%;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  background-color: rgba(255, 255, 255, 0.3);
  animation-duration: 8s;
}

.bubble-bgwall li:nth-child(5) {
  left: 70%;
}

.bubble-bgwall li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  background-color: rgba(255, 255, 255, 0.2);
  animation-delay: 3s;
}

.bubble-bgwall li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(8) {
  left: 55%;
  width: 40px;
  height: 40px;
  font-size: 12px;
  animation-duration: 15s;
  animation-delay: 4s;
}

.bubble-bgwall li:nth-child(9) {
  left: 25%;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.3);
  font-size: 12px;
  animation-duration: 12s;
  animation-delay: 2s;
}

.bubble-bgwall li:nth-child(10) {
  left: 85%;
  width: 160px;
  height: 160px;
  animation-delay: 5s;
}

@keyframes bubble {
  0% {
    opacity: 0.5;
    transform: translateY(0) rotate(45deg);
  }
  25% {
    opacity: 0.75;
    transform: translateY(-400px) rotate(90deg);
  }
  50% {
    opacity: 1;
    transform: translateY(-600px) rotate(135deg);
  }
  100% {
    opacity: 0;
    transform: translateY(-1000px) rotate(180deg);
  }
}

/* 登录容器 - 右下角定位 */
.login-container {
  position: fixed;
  right: 100px;
  bottom: 100px;
  width: 420px;
  z-index: 1000;
  animation: fadeIn 0.6s ease;
}

/* 登录框 */
.login-box {
  background: white;
  border-radius: 15px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  width: 100%;
  overflow: hidden;
  backdrop-filter: blur(10px);
}

/* 登录头部 */
.login-header {
  padding: 35px 30px 25px;
  text-align: center;
  background: linear-gradient(135deg, #a373ca, #8e44ad);
  color: white;
  position: relative;
  overflow: hidden;
}

.login-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 100%);
  pointer-events: none;
}

.login-header h1 {
  font-size: 26px;
  font-weight: 700;
  margin-bottom: 8px;
  position: relative;
  z-index: 1;
}

.login-header .subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 20px;
  position: relative;
  z-index: 1;
}

/* 测试账号提示 */
.test-accounts {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  padding: 15px;
  margin-top: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 1;
}

.test-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #fff;
}

.test-account {
  font-size: 12px;
  margin: 4px 0;
  color: rgba(255, 255, 255, 0.9);
  font-family: monospace;
}

/* 最近注册用户提示 */
.recent-user {
  background: rgba(76, 217, 100, 0.15);
  border-radius: 8px;
  padding: 15px;
  margin-top: 15px;
  border: 1px solid rgba(76, 217, 100, 0.3);
  position: relative;
  z-index: 1;
}

.recent-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #fff;
}

.recent-account {
  font-size: 12px;
  margin: 4px 0;
  color: rgba(255, 255, 255, 0.9);
  font-family: monospace;
  font-weight: bold;
}

.recent-note {
  font-size: 11px;
  margin-top: 8px;
  color: rgba(255, 255, 255, 0.8);
  font-style: italic;
  border-top: 1px solid rgba(76, 217, 100, 0.2);
  padding-top: 8px;
}

/* 登录表单 */
.login-form {
  padding: 35px 30px;
}

.form-group {
  margin-bottom: 25px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #e1e8ed;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.form-group input:focus {
  border-color: #a373ca;
  outline: none;
  box-shadow: 0 0 0 2px rgba(163, 115, 202, 0.2);
}

.form-group input:hover {
  border-color: #a373ca;
  box-shadow: 0 2px 12px rgba(163, 115, 202, 0.2);
}

/* 密码输入框和眼睛图标 */
.password-container {
  position: relative;
}

.eye-button {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: #666;
  padding: 4px;
  font-size: 16px;
  transition: color 0.3s ease;
}

.eye-button:hover {
  color: #a373ca;
}

/* 表单底部 */
.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.remember-me {
  display: flex;
  align-items: center;
}

.remember-me input {
  margin-right: 8px;
  width: auto;
}

.remember-me label {
  margin-bottom: 0;
  font-size: 14px;
  color: #666;
  cursor: pointer;
}

.forgot-password {
  color: #a373ca;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #8e44ad;
  text-decoration: underline;
}

/* 按钮容器 */
.button-container {
  display: flex;
  gap: 15px;
  margin-top: 25px;
}

/* 登录按钮 */
.login-button {
  flex: 1.5;
  padding: 14px 20px;
  background: linear-gradient(135deg, #a373ca, #8e44ad);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(163, 115, 202, 0.3);
}

.login-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #8e44ad, #7d3c98);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(163, 115, 202, 0.4);
}

.login-button:disabled {
  opacity: 0.8;
  cursor: not-allowed;
  transform: none;
}

/* 注册按钮 */
.register-button {
  flex: 1;
  padding: 14px 20px;
  background: white;
  color: #a373ca;
  border: 2px solid #a373ca;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.register-button:hover {
  background: #a373ca;
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(163, 115, 202, 0.3);
}

/* 错误消息 */
.error-message {
  margin-top: 20px;
  padding: 12px 16px;
  background: linear-gradient(135deg, #ffebee, #ffcdd2);
  color: #c62828;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  border: 1px solid #ef9a9a;
  animation: shake 0.5s ease;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    right: 5%;
    left: 5%;
    bottom: 50%;
    transform: translateY(50%);
    width: auto;
  }
  
  .login-box {
    max-width: 400px;
    margin: 0 auto;
  }
  
  .login-header {
    padding: 25px 20px 20px;
  }
  
  .login-form {
    padding: 25px 20px;
  }
  
  .button-container {
    flex-direction: column;
  }
  
  .login-button,
  .register-button {
    flex: 1;
    width: 100%;
  }
}

@media (max-width: 480px) {
  .bubble-bgwall li {
    display: none;
  }
  
  .bubble-bgwall li:nth-child(1),
  .bubble-bgwall li:nth-child(3),
  .bubble-bgwall li:nth-child(5) {
    display: flex;
  }
  
  .login-container {
    right: 10px;
    left: 10px;
  }
}
</style>
